package leetcode.offer;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Stack;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021/11/08
 */
public class offer30 {

    private static final Logger logger = LoggerFactory.getLogger(offer30.class);

    @Test
    public void test() {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        logger.info("min:" + minStack.min());
        minStack.pop();
        logger.info("top:" + minStack.top());
        logger.info("min:" + minStack.min());

    }


}

class MinStack {

    Stack<Integer> stack;
    private int min = Integer.MAX_VALUE;

    public MinStack() {
        stack = new Stack<>();
    }

    public void push(int x) {
        if (x <= min) {
            stack.push(x);
            min = x;
            return;
        }
        stack.push(x);
    }

    public void pop() {
        stack.pop();
        min = stack.stream().sorted().findFirst().orElse(Integer.MAX_VALUE);
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return min;
    }
}
